﻿using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 剧情设计器2._0
{
    public partial class CharactersManager : Form
    {
        public CharactersManager()
        {
            InitializeComponent();
        }

        void UpdateDisplay()
        {
            listBox_characters.BeginUpdate();
            listBox_characters.Items.Clear();
            foreach(Character c in Common.characters)
            {
                listBox_characters.Items.Add(c.name);
            }
            listBox_characters.EndUpdate();
        }

        private void CharactersManager_Load(object sender, EventArgs e)
        {
            UpdateDisplay();
        }

        private void listBox_characters_SelectedIndexChanged(object sender, EventArgs e)
        {
            int idx = listBox_characters.SelectedIndex;
            if (idx < 0) return;
            Character c = Common.characters[idx];
            edit_name.Text = c.name;
            listView_settings.BeginUpdate();
            listView_settings.Items.Clear();
            foreach(Setting setting in c.settings)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Text = setting.name;
                lvi.SubItems.Add(setting.type);
                lvi.SubItems.Add(setting.value);
                listView_settings.Items.Add(lvi);
            }
            listView_settings.EndUpdate();
        }

        private void createClick(object sender, EventArgs e)
        {
            Common.characters.Add(new Character() { name = "新角色" });
            UpdateDisplay();
        }

        private void EditCharacter(object sender, MouseEventArgs e)
        {
            int idx = listBox_characters.SelectedIndex;
            if (idx < 0) return;
            CharacterEditor ce = new CharacterEditor(Common.characters[idx]);
            ce.Show();
            UpdateDisplay();
        }

        private void deleteClick(object sender, EventArgs e)
        {
            int idx = listBox_characters.SelectedIndex;
            if (idx < 0) return;
            if(MessageBox.Show(string.Format("确定要删除角色 {0} 吗？", Common.characters[idx].name), "删除角色", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                Common.characters.RemoveAt(idx);
                UpdateDisplay();
            }
        }

        private void btn_copy_Click(object sender, EventArgs e)
        {
            int idx = listBox_characters.SelectedIndex;
            if (idx < 0) return;
            Character c = Common.DeepCopy(Common.characters[idx]);
            c.name += " - 副本";
            Common.characters.Add(c);
            UpdateDisplay();
        }
    }
}
